package com.xnyzc.lhy.didi.service.impl.xiecheng;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xnyzc.lhy.common.component.redis.RedisCommon;
import com.xnyzc.lhy.common.constant.RedisPrefixConstant;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.didi.entity.price.CharterRulesParam;
import com.xnyzc.lhy.didi.mapper.price.OaSysBillSpecialEmbargoMapper;
import com.xnyzc.lhy.didi.mapper.price.OaSysNewBillRuleMapper;
import com.xnyzc.lhy.didi.service.xiecheng.EmbargoAreaService;
import com.xnyzc.lhy.didi.service.xiecheng.XcPriceService;
import com.xnyzc.lhy.mis.entity.system.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class EmbargoAreaServiceImpl implements EmbargoAreaService {
    @Autowired
    private RedisCommon redisCommon;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private OaSysBillSpecialEmbargoMapper oaSysBillSpecialEmbargoMapper;
    @Autowired
    private XcPriceService xcPriceService;
    @Autowired
    private OaSysNewBillRuleMapper oaSysNewBillRuleMapper;

    @Override
    public List<NewBillRules> getPriceDetail(Integer channel, String cityCode, Integer callType, Integer rulesNo) {
        List<NewBillRules> billRules = null;
        String key = RedisPrefixConstant.BILL_RULES + "_" + channel + "_" + cityCode + "_" + callType + "_" + rulesNo;
        String oaSysBillRulesJson = redisCommon.getStr(key);
        if (CheckUtil.strIsNotEmpty(oaSysBillRulesJson)) {
            billRules = JSON.parseArray(oaSysBillRulesJson, NewBillRules.class);
        }
        //如果计价规则为空,继续向数据库寻找
        if (billRules == null || billRules.size() == 0) {
            // 查询普通时间
            if (rulesNo == null) {
                billRules = oaSysNewBillRuleMapper.selectList3(callType, Long.parseLong(cityCode), channel);
            } else {
                billRules = oaSysNewBillRuleMapper.selectList2(callType, Long.parseLong(cityCode), channel, rulesNo);
            }
            redisCommon.set(key, JSON.toJSONString(billRules));
        }
        return billRules;
    }

    @Override
    public List<NewBillRules> getCharterRules(Integer channel, String cityCode) {
        List<NewBillRules> newBillRulesList = new ArrayList<>();
        String key = RedisPrefixConstant.BILL_RULES_CHARTERED + "_" + channel + "_" + cityCode;
        HashMap<String, String> entries = redisCommon.getHashMap(key);
        if (!entries.isEmpty()){
            for (Map.Entry<String, String> entry : entries.entrySet()) {
                String entryV =  entry.getValue();
                NewBillRules newBillRules = JSON.parseObject(entryV,NewBillRules.class);
                newBillRulesList.add(newBillRules);
            }
            return newBillRulesList;
        }
        Map<String, String> map =new HashMap();
        List<NewBillRules> nbs= oaSysNewBillRuleMapper.selectCharterRules(channel,Long.parseLong(cityCode));
        for (int i = 0; i < nbs.size(); i++) {
            NewBillRules nb=nbs.get(i);
            String putK=nb.getVehicleType()+":"+nb.getRulesName();
            map.put(putK, JSONObject.toJSONString(nb));
            newBillRulesList.add(nb);
        }
        redisTemplate.opsForHash().putAll(key,map);
        return newBillRulesList;
    }
}
